package org.huangrui.spark.scala.core.rdd.dep

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 * @Author hr
 * @Create 2024-10-19 11:24
 */
object Spark03_Dep {
  def main(args: Array[String]): Unit = {
    val sparConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
    val sc = new SparkContext(sparConf)

    val lines: RDD[String] = sc.makeRDD(Array("hello world", "hello atguigu", "atguigu", "hahah"))
    val words: RDD[String] = lines.flatMap(_.split(" "))
    val wordToOne = words.map(word => {
      println("***********************")
      (word, 1)
    })
    val wordToSum: RDD[(String, Int)] = wordToOne.reduceByKey(_ + _)
    val array: Array[(String, Int)] = wordToSum.collect()

    println("计算1完毕")
    array.foreach(println)
    println("#########################")
    wordToOne.groupByKey().collect()
    println("计算2完毕")

    sc.stop()

  }
}
